//index.js
const app = getApp()
let orderByname = 'createTime'
// 搜索的关键字
let keyword = ''
let lasttime = 0
const db = wx.cloud.database()
Page({
  data: {
    blog: [],
    swiperImgUrls: [],
    navItems: [{
      name: '最新',
      index: 1
    }, {
      name: '热门',
      index: 2
    }],
    tabCur: 1,
  },

  // 获取轮播图
  _getSwiper() {
    db.collection('blog').where({
        state: true
      }).limit(5)
      .orderBy("likeNum", "desc")
      .orderBy("collectNum", "desc")
      .orderBy("commentNum", "desc")
      .get().then((res) => {
        this.setData({
          swiperImgUrls: res.data
        })
      })
  },
  // 默认的博客
  _getgetPostsList: function (start = 0, orderByname = 'createTime') {
    wx.cloud.callFunction({
      name: 'seek',
      data: {
        keyword,
        start,
        orderByname,
        count: 5,
        $url: 'postslist'
      }
    }).then((res) => {
      this.setData({
        blog: this.data.blog.concat(res.result)
      })
    })
  },
  onLoad: function (options) {
    this._getSwiper()
  },
  tabSelect: function (e) {
    //-----防抖
    let d = new Date()
    let nowtime = d.getTime() //获取点击时间
    if (nowtime - lasttime > 800) {
      //800ms内无法识别再点击
      this.setData({
        tabCur: e.target.dataset.id
      })
      if (e.target.dataset.data.name == '最新') {
        orderByname = 'createTime'
        this.setData({
          blog: []
        })
        this._getgetPostsList(0, orderByname)
      } else {
        orderByname = 'commentNum'
        this.setData({
          blog: []
        })
        this._getgetPostsList(0, orderByname)
      }
    }
    lasttime = nowtime
    //-----防抖
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      swiperImgUrls: [],
      blog: []
    })
    this._getSwiper()
    this._getgetPostsList(0)
    setTimeout(() => {
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading();
    }, 1000)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this._getgetPostsList(this.data.blog.length, orderByname)
  },
  // 跳转
  goComment: function (e) {
    // console.log(e.target.dataset)
    let name = e.target.dataset.name
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    if (name == '文章') {
      // 跳转到文章
      this.gettoyblog(e.target.dataset.blogid)
    } else {
      // 跳转其他全部
      this.gettolblog(e.target.dataset.blogid)
    }
  },
  gettoyblog(e) {
    wx.hideLoading()
    wx.navigateTo({
      url: `../blog-academic/blog-academic?blogid=${e}`,
    })
  },
  gettolblog(e) {
    wx.hideLoading()
    wx.navigateTo({
      url: `../blog-plain/blog-plain?blogid=${e}`,
    })
  },
  //搜索
  bindconfirm: function (e) {
    // console.log(e.detail.value)
    this.setData({
      blog: []
    })
    keyword = e.detail.value
    this._getgetPostsList(0, orderByname)
  },

  onShow: function (e) {
    this.setData({
      blog: []
    })
    this._getgetPostsList(0)
  }
})